package com.example.tally.utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import androidx.annotation.NonNull;
import com.example.tally.R;
import com.example.tally.adapter.CalendarAdapter;
import com.example.tally.db.DBManager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author:0.2℃
 * @date： 2021-07-16 16:45
 * @Description ：
 */

public class CalendarDialog extends Dialog implements View.OnClickListener {
    ImageView errorIv;
    GridView gv;
    LinearLayout hsvLayout;
    List<TextView> hsvViewList;
    List<Integer> yearList;
    CalendarAdapter adapter;
    // 表示被点击的位置
    int selectPos = -1;
    int selectMonth = -1;

    OnRefreshListener onRefreshListener;

    public CalendarDialog(@NonNull Context context , int selectPos , int selectMonth) {
        super(context);
        this.selectPos = selectPos;
        this.selectMonth = selectMonth;
    }

    public interface OnRefreshListener{
        public void Ensure(int selPos , int year , int month);
    }

    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
        this.onRefreshListener = onRefreshListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_calendar);
        errorIv = findViewById(R.id.dialog_calendar_error);
        gv = findViewById(R.id.dialog_calendar_gv);
        hsvLayout = findViewById(R.id.dialog_calendar_layout);
        errorIv.setOnClickListener(this);
        // 向横向的ScrollView 当中添加View的方法
        addViewToLayout();
        initGridView();
        // 设置GridView当中每一个Item的点击事件
        setGVListener();
    }

    private void setGVListener() {
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.selectPos = position;
                adapter.notifyDataSetChanged();;
                int month = position + 1;
                int year = adapter.year;
                // 获取到被选中的年份和月份
                onRefreshListener.Ensure(selectPos , year , month);
                cancel();
            }
        });
    }

    private void initGridView() {
        int selectYear = yearList.get(selectPos);
        adapter = new CalendarAdapter(getContext(), selectYear);
        if (selectMonth == -1) {
            int month = Calendar.getInstance().get(Calendar.MONTH);
            adapter.selectPos = month;
        }else{
            adapter.selectPos = selectMonth - 1;
        }
        gv.setAdapter(adapter);
    }

    private void addViewToLayout() {
        // 将添加进入线性布局当中的TextView进行统一布局
        hsvViewList = new ArrayList<>();
        // 获取数据库当中的年份
        yearList = DBManager.yearListFromAccounttb();
        // 如果数据库中的年份为空，则将今年的年份添加进去
        if (yearList.size() == 0) {
            int year = Calendar.getInstance().get(Calendar.YEAR);
            yearList.add(year);
        }

        // 遍历年份,有几年就向ScrollView当中添加几个View
        for (int i = 0; i < yearList.size(); i++) {
            int year = yearList.get(i);
            // 将需要添加的布局文件转换为View
            View view = getLayoutInflater().inflate(R.layout.item_dialogcal_hsv, null);
            hsvLayout.addView(view);
            TextView hsvTv = view.findViewById(R.id.item_dialocal_hsv_tv);
            hsvTv.setText(year + "");
            hsvViewList.add(hsvTv);
        }
        // 第一次点击去之后选中的是最近的年份
        if(selectPos == -1){
            selectPos = hsvViewList.size() - 1;
        }
        changTvBg(selectPos); // 将最后一个设置为选中状态
        setHsvClickListener();
    }

    /**给横向的ScrollView当中所有的TextView设置点击事件*/
    private void setHsvClickListener() {
        for (int i = 0; i < hsvViewList.size(); i++) {
            TextView view = hsvViewList.get(i);
            int pos = i;
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    changTvBg(pos);
                    selectPos = pos;
                    // 获取被选中的年份，下面的GridView显示数据源会发生变化
                    int year = yearList.get(selectPos);
                    adapter.setYear(year);
                }
            });
        }
    }

    /**传入被选中的位置，改变此位置上背景和颜色*/
    private void changTvBg(int selectPos) {
        for (int i = 0; i < hsvViewList.size(); i++) {
            TextView tv = hsvViewList.get(i);
            tv.setBackgroundResource(R.drawable.dialog_btn_bg);
            tv.setTextColor(Color.BLACK);
        }
        TextView view = hsvViewList.get(selectPos);
        view.setBackgroundResource(R.drawable.main_recordbtn_bg);
        view.setTextColor(Color.WHITE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dialog_calendar_error:
                cancel();
                break;
        }
    }
    /**
     * 设置Dialog的尺寸和屏幕尺寸一致
     */
    public void setDialogSize(){
        // 获取当前窗口对象
        Window window = getWindow();
        // 获取窗口对象的参数
        WindowManager.LayoutParams wlp = window.getAttributes();
        // 获取屏幕宽度
        Display d = window.getWindowManager().getDefaultDisplay();
        wlp.width = (int)d.getWidth(); // 对话框窗口为屏幕窗口
        wlp.gravity = Gravity.TOP;   // 从顶部弹出
//        window.setBackgroundDrawableResource(android.R.color.transparent);  // 设置为透明
        window.setAttributes(wlp);
    }
}
